package com.ruiyi.controller;

import com.ruiyi.common.Constants;
import com.ruiyi.common.Result;
import com.ruiyi.dto.AccountDto;
import com.ruiyi.service.AccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import javax.validation.Valid;

@RestController
@RequestMapping("/account")
public class AccountController {

    @Autowired
    private AccountService accountService;

    @RequestMapping("/getLogin.do")
    public Result getLogin(@Valid AccountDto accountDto, BindingResult br, HttpSession session){
        if(br.hasErrors()){
            //显示前端传参的错误信息
            return Result.DATA_FORMAT_ERROR;
        }
        return accountService.findLogin(accountDto,session);
    }

    @RequestMapping("/loginToOut.do")
    public Result loginToOut(HttpSession session){
        //退出之前，先清空保存在后端缓存里面的数据
        session.removeAttribute(Constants.USER_SESSION_KEY);
        return new Result();
    }

    @PostMapping("/getAccountsByPage.do")
    public Result getAccountsByPage(
            Integer pageNum,
            @RequestParam(required = false,defaultValue = "10") Integer pageSize
    ){
        return accountService.findAccountsByPage(pageNum,pageSize);
    }

    @PostMapping("/editAccountsStatus.do")
    public Result editAccountsStatus(Integer status,Long id,HttpSession session){
        return accountService.modifyAccountStatus(status,id,session);
    }

    @PostMapping("/addAccount.do")
    public Result addAccount(String username,HttpSession session){
        return accountService.saveAccount(username,session);
    }

    @PostMapping("/cutOneAccount.do")
    public Result cutOneAccount(Long id,HttpSession session){
        return accountService.removeAccountById(id,session);
    }

    @PostMapping("/editResetAccountPwd.do")
    public Result editResetAccountPwd(Long id,HttpSession session){
        return accountService.modifyResetAccountPwd(id,session);
    }

    @PostMapping("/editAccountPwd.do")
    public Result editAccountPwd(String pwd,HttpSession session){
        return accountService.modifyAccountPwd(pwd,session);
    }

}
